package cn.sq.musicserver.scheduling.job.controller;


import cn.sq.musicserver.config.ResponseBean;
import cn.sq.musicserver.config.StatusCode;
import cn.sq.musicserver.scheduling.job.job.MuiscScanJob;
import cn.sq.musicserver.scheduling.job.service.QuartzService;
import cn.sq.musicserver.set.entity.SqSet;
import cn.sq.musicserver.set.service.impl.SqSetServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.DynamicParameter;
import com.github.xiaoymin.knife4j.annotations.DynamicParameters;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: SQ
 * Date: 2019/4/11
 * Time: 17:00
 * Description:
 */
@Slf4j
@RestController
@Api(value = "定时任务",tags = {"定时任务"})
@RequestMapping(value = "/quartz",method = RequestMethod.POST)
public class QuartzController {

    @Autowired
    private QuartzService quartzService;
    @Autowired
    private SqSetServiceImpl setService;
//    @RequestMapping("/addjob")
//    public ResponseBean startJob() {
//
//
//    }

    @ApiOperation(value = "添加音乐扫描1",notes = "添加音乐扫描根据指定未知")
    @RequestMapping("/addjob")
    public ResponseBean addjob() {
        boolean b = quartzService.addJob(MuiscScanJob.class, "scanMuisc", "scanMuisc",setService.getOne(new QueryWrapper<SqSet>().eq("SetName","SystemScanTime")).getSetVlaue());
        return StatusCode.successResponseBean(b);
    }
    @ApiOperation(value = "修改扫描频率",notes = "修改扫描频率，请勿使用，请在设置出进行修改")
    @ApiOperationSupport(params = @DynamicParameters(name = "网络音乐搜索", properties = {
            @DynamicParameter(name = "cron时间表达式", value = "corn 与keywords二选一传递", example = "0 0 0 /1 * ? *", required = true, dataTypeClass = String.class),
    }))
    @RequestMapping("/updatejob")
    public ResponseBean updatejob(@RequestBody Map<String, String> map) {
        String corn = map.get("corn");
        boolean b = quartzService.updateJob("scanMuisc", "scanMuisc", corn);
        return StatusCode.successResponseBean(b);
    }
    @ApiOperation(value = "删除歌曲扫描",notes = "删除歌曲扫描")
    @RequestMapping("/deletejob")
    public ResponseBean deletejob() {
        boolean b = quartzService.deleteJob("scanMuisc", "scanMuisc");
        return StatusCode.successResponseBean(b);
    }
    @ApiOperation(value = "暂停歌曲扫描",notes = "暂停歌曲扫描")
    @RequestMapping("/pauseJob")
    public ResponseBean pauseJob() {
        boolean b = quartzService.pauseJob("scanMuisc", "scanMuisc");
        return StatusCode.successResponseBean(b);

    }
    @ApiOperation(value = "开始歌曲扫描",notes = "开始歌曲扫描")
    @RequestMapping("/resumeJob")
    public ResponseBean resumeJob() {
        boolean b = quartzService.resumeJob("scanMuisc", "scanMuisc");
        return StatusCode.successResponseBean(b);

    }
    @ApiOperation(value = "查询所有扫描",notes = "查询所有扫描")
    @RequestMapping("/queryAllJob")
    public ResponseBean queryAllJob() {
        List<Map<String, Object>> maps = quartzService.queryAllJob();
        return StatusCode.successResponseBean(maps);
    }
    @ApiOperation(value = "查询正在运行的扫描",notes = "查询正在运行的扫描")
    @RequestMapping("/queryRunJob")
    public ResponseBean queryRunJob() {
        List<Map<String, Object>> maps = quartzService.queryRunJob();
        return StatusCode.successResponseBean(maps);
    }
    @ApiOperation(value = "立即执行一次扫描1",notes = "立即执行一次扫描1")
    @RequestMapping("/runAJobNow")
    public ResponseBean runAJobNow() {
        boolean b = quartzService.runAJobNow("scanMuisc", "scanMuisc");
        return StatusCode.successResponseBean(b);
    }


}
